/**
 * created by 小卷毛, 2020-06-28 028
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.zhiyou.aifeng.viewmodel

import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lzg.okrx.RxSingleHelper
import com.zhiyou.aifeng.common.helper.getJsonFromAssets
import com.zhiyou.aifeng.common_ui.model.AreaData
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.utils.applyFlowableSchedulers
import com.zhiyou.aifeng.common_ui.utils.applySchedulers
import com.zhiyou.aifeng.common_ui.viewmodel.BaseViewModel
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 * 项目名称：Smart_Machine
 * 创建人：小卷毛
 * 创建时间：2020-06-28 18:22
 */
class LoginViewModel : BaseViewModel() {

    fun getCode(phone: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.getCode,
            params = getParams(
                "phone" to phone,
                "type" to 4,
                "role" to 2
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun login(phone: String, code: String, pushId: String) =
        RxSingleHelper.requestString(
            url = BaseUrl.login,
            params = getParams(
                "phone" to phone,
                "code" to code,
                "role" to 2,
                "jpushid" to pushId
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun countDown(count: Long = 180): Flowable<Long> =
        Flowable.intervalRange(0, count + 1, 0, 1, TimeUnit.SECONDS)
            .compose(applyFlowableSchedulers())

    fun apply(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.apply,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun getAreaList(context: Context): Flowable<ArrayList<AreaData>> =
        Flowable.just("district_data.json")
            .subscribeOn(Schedulers.io())
            .map {
                val json = context.getJsonFromAssets(it)
                if (json.isEmpty()) return@map ArrayList<AreaData>()
                return@map Gson().fromJson(
                    json,
                    object : TypeToken<List<AreaData>>() {}.type
                ) as ArrayList<AreaData>
            }
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}